package cn.com.gome.framework.utils;


import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
 * @ClassName: cn.com.gome.framework.utils
 * @Description: 数据源切换
 * Created by wangheming on 2017/12/1 10:35.
 */
public class DataSourceExchange implements MethodBeforeAdvice,AfterReturningAdvice {

    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        SwitchDataSourceTools.clearDataSource();
    }

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        // 这里的DataSource是自定义的注解，不是java里的DataSource接口
        if (method.isAnnotationPresent(DataSource.class))
        {
            // 设置数据库类型
            DataSource datasource = method.getAnnotation(DataSource.class);
            SwitchDataSourceTools.setThkey(datasource.name());
        }
        else
        {
            //target是被织入增强处理的目标对象，通过获取getDataSourceName函数来获取target的数据源名称
            /*个人理解为默认的值*/
//            SwitchDataSourceTools.setThkey(target.getClass.getMethod("getDataSourceName").invoke(target).toString());
//            SwitchDataSourceTools.setThkey(SinoConstant.DataSourceType.unityDataSource.toString());
            SwitchDataSourceTools.setThkey("");
        }
    }
}
